//
//  CarQueryHomeOutViewController.m
//  XTCloud
//
//  Created by QA on 2017/3/2.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "CarQueryHomeOutViewController.h"
#import "CarQueryHomeViewCell.h"
#import "GETableView.h"

#import "WebServiceCommonRequest.h"
#import "GetUserStationService.h"

#import "UIView+AssistView.h"
#import "NSDictionary+DataFormat.h"

@interface CarQueryHomeOutViewController ()

@property(nonatomic ,strong) IBOutlet GETableView * tableView;
@property (strong, nonatomic) NSMutableArray *resultData;

@end

@implementation CarQueryHomeOutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.resultData = [NSMutableArray array];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
    
    [self getNetWorkData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//更新页面数据
- (void)updateViewData
{
    [self.tableView removeAllSaveCell];
    [self.resultData removeAllObjects];
    
    [self.tableView reloadData];
    [self getNetWorkData];
}

#pragma mark 网络获取数据

- (void)getNetWorkData
{
    //显示进度条
    [self.tableView showViewProgressRect:CGRectMake(0, 0, SuperWidth, SuperHeight - 320)];
    
    //站点ID
    StationInfoEntity *model = [[GetUserStationService sharedService] getSelectStationEntity];
    
    //@状态（全部、供毕、正供、待供）
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeERP];
    [content setModuleType:@"CLDT_HOME_CLXX_YFCL_20171117"];
    [content setStationID:[model.stationId integerValue]];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        //添加数据，刷新页面
        NSArray *dataArray1 = [NSArray array];
        NSArray *dataArray2 = [NSArray array];
        if(result.resultArray && result.resultArray.count>=2){
            dataArray1 = result.resultArray[0];
            dataArray2 = result.resultArray[1];
        }
        
        NSMutableArray *resultNewArray = [NSMutableArray array];
        for(int i=0;i<dataArray1.count;i++){
            NSDictionary *dictionary = [dataArray1 objectAtIndex:i];
            NSString *gcID = [dictionary objectFormatStringForKey:@"任务单编号"];
            
            NSMutableDictionary *nowDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];
            NSMutableArray *array = [NSMutableArray array];
            for(int j=0;j<dataArray2.count;j++){
                NSDictionary *dictionaryItem = [dataArray2 objectAtIndex:j];
                NSString *gcIDItem = [dictionaryItem objectFormatStringForKey:@"任务单编号"];
                if([gcID isEqualToString:gcIDItem]) [array addObject:dictionaryItem];
            }
            
            if(array.count<=0) continue;//删除没有车辆的任务单
            [nowDictionary setObject:array forKey:@"车辆信息"];
            [resultNewArray addObject:nowDictionary];
        }
        
        if(resultNewArray && resultNewArray.count>0){
            self.resultData = resultNewArray;
            [self.tableView removeAllCustomFromSuperview];
            [self.tableView reloadData];
        } else {
            [self.tableView showPromptViewText:@"暂无车辆信息" rect:CGRectMake(0, 0, SuperWidth, SuperHeight - 280) blcok:nil];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self errorInfoViewText:@"哦，出错了"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self errorInfoViewText:@"无法连接网络"];
    }];
}

- (void)errorInfoViewText:(NSString*)label
{
    [self.tableView showPromptViewText:label rect:CGRectMake(0, 0, SuperWidth, SuperHeight - 280) blcok:^{
        [self updateViewData];
    }];
}

#pragma mark - Table view data source

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.resultData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *keyValue = [NSString stringWithFormat:@"ad-%ld-%ld",indexPath.section,indexPath.row];
    CarQueryHomeViewCell *cell = [self.tableView getCellViewKey:keyValue];
    if (cell) return cell;
    
    cell = [[CarQueryHomeViewCell alloc] initWithChangeWidthStyle:UITableViewCellStyleDefault reuseIdentifier:keyValue];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSDictionary *mDictionary = [self.resultData objectAtIndex:indexPath.row];
    [cell drawCarViewContent:mDictionary];
    [self.tableView setCellView:cell key:keyValue];
    return cell;
}

#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dictionary = [self.resultData objectAtIndex:indexPath.row];
    NSArray *array = [dictionary objectForKey:@"车辆信息"];
    return [CarQueryHomeViewCell getCellHeight:array.count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
